<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use dosamigos\datetimepicker\DateTimePicker;
use common\helper\HashHelper;


/* @var $this yii\web\View */
/* @var $model backend\models\CommentsRelation */
/* @var $m backend\models\CommentsSearch*/
/* @var $form yii\widgets\ActiveForm */
if($model->isNewRecord){
    $model->flag = 1;
    $model->is_show = 1;
    $model->level = 5;
    //$model->create_time = $m->create_time = date('Y-m-d h:i:s');
}

$time = time();
$key = HashHelper::getUploadHash($time);

?>
<style>
#comments-relation-form textarea{width: 200px;resize: none;}
</style>
<div class="comments-relation-form">

    <?php
    \Yii::$container->set(\yii\widgets\ActiveField::className(), ['template' => "{label}\n{input}\n{hint}"]);
    $form = ActiveForm::begin([
        'enableClientValidation' => false,
        'enableClientScript' => false,
        'method' => 'post',
        'options' => ['class' => 'form-inline'],
    ]); ?>

    <?php if($model->isNewRecord){?>
        <?= $form->field($model, 'bestry_uid')->textInput() ?>
        <?= $form->field($model, 'goods_id')->textInput() ?>
        <?= $form->field($model, 'status')->dropDownList(\common\models\CommentsRelation::$STATUS_A)?>
        <?= $form->field($model, 'is_show')->dropDownList(\common\models\CommentsRelation::$IS_SHOW)?>
        <?= $form->field($model, 'flag')->hiddenInput()->label(false)?>
    <?php }else{?>
        <?= $form->field($model, 'bestry_uid')->textInput(['readonly'=>true]) ?>
        <?= $form->field($model, 'status')->hiddenInput()->label(false)?>
        <?= $form->field($model, 'is_show')->hiddenInput()->label(false)?>
    <?php }?>
    <?= $form->field($model, 'level')->dropDownList(\common\models\CommentsRelation::$LEVEL) ?>

    <?= $form->field($m, 'create_time')->widget(DateTimePicker::className(),
        [
            'template'=>"{input}{reset}{button}",
            'clientOptions' => [
                'autoclose' => true,
                //'linkFormat' => 'yyyy-MM-dd h:i:s', // if inline = true
                'format' => 'yyyy-mm-dd h:i:s', // if inline = false
                'todayBtn' => true,
                'pickerPosition'=>"bottom-left",
                'language'=>'zh-CN',
                'minView'=>'month'
            ]
        ])?>
    <?= $form->field($model,'create_time')->hiddenInput()->label(false)?>
    <br/><br/>
    <?= $form->field($m, 'content')->textarea(['style'=>'width:600px;height:100px;resize: none;', 'maxlength'=>1024]) ?>
    <?php //if($model->isNewRecord){?>
    <div>
        <?= Html::label('评价内容需超过20字，不可使用全符号/全英文/全数字，同一字符不可超过五次，汉字至少10个。','',
            ['style'=>'color:#a94442;font-size:small;padding-left: 65px','id'=>'t', 'class'=>'t'])?>
    </div>
    <?php //}?>
    <br/><br/>

    <?= $form->field($m,'img')->widget('yidashi\webuploader\WebBatchUploader',
        ['server'=>common\helper\UrlManager::createUrl(Yii::$app->params['domains']['upload'],['upload/webupload','time'=>$time,'key'=>$key]), 'options'=>['number'=>4]]); ?>
    <br>
    <?= Html::label('图片大小不得超过2M', '', ['style'=>'color:#a94442;font-size:small'])?>
    <br/><br/>
    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? '确认创建' : '确认修改', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>
<?php $this->beginBlock('js'); ?>
<script>

        $('[name="CommentsSearch[create_time]"]').change(function(){

            var create_time = $(this).val();
            $('#commentsrelation-create_time').val(create_time);

        });

        function countByLastChar(str){
            var textStr = str.charAt(str.length-1),
                count = 0;
            for(var i = textStr.length-5; i < str.length; i++){
                if(str.charAt(i) == textStr){
                    count++;
                }
            }
            return count;
        }

        $("#commentssearch-content").keyup(function(){
            var content = $(this).val();
            content = content.replace(/\s/g,'');

            var reg = /^\d*$/;
            var reg1 = /^[a-zA-Z]*$/i;
            var reg2 = /^[\u3002\uff1b\uff0c\uff1a\u201c\u201d\uff08\uff09\u3001\uff1f\u300a\u300b\！\,\.\;\:\"\'\?\!\<\>]*$/;

            //除掉标点符号，至少10个汉字
            var char_str=content.replace(/[\！|\、|\—|\；|\（|\）|\：|\·|\“|\‘|\”|\’|\【|\】|\，|\《|\。|\》|\？]/g,"");
            var i = 0;
            for(var a = 0;a < char_str.length;a++)
            {
                if (char_str.charCodeAt(a)>255)
                {
                    i++;
                }
            }

            if(reg.test(content) || reg1.test(content) || reg2.test(content) || i < 10)
            {

                $('.field-commentssearch-content').attr("class", 'form-group field-commentssearch-content has-error');
                $('.t').show();
            }
            else
            {
                if(countByLastChar(content) >= 5)
                {
                    $('.field-commentssearch-content').attr("class", 'form-group field-commentssearch-content has-error');
                    $('.t').show();
                }
                else
                {
                    $('.field-commentssearch-content').attr("class", 'form-group field-commentssearch-content has-success');
                    $('.t').hide();
                }

            }
        });


        $("#w0").submit(function(){
            var bestry_uid = $('#commentsrelation-bestry_uid').val();
            var goods_id = $('#commentsrelation-goods_id').val();
            var status = $('#commentsrelation-status').val();
            var is_show = $('#commentsrelation-is_show').val();
            var level = $('#commentsrelation-level').val();
            var create_time = $('#commentsrelation-create_time').val();
            var content = $('#commentssearch-content').val();

            if(bestry_uid == '' || bestry_uid < 1 || isNaN(bestry_uid)){
                alert('用户ID不合法！');
                return false;
            }
            <?php if($model->isNewRecord){?>
            if(goods_id == '' || goods_id < 1 || isNaN(goods_id)){
                alert('商品ID不合法！');
                return false
            }
            <?php }?>
            if(is_show == '' ){
                alert('请选择评论是否展示！');
                return false;
            }
            if(level == ''){
                alert('请选择评价星级！');
                return false;
            }
            if(create_time == ''){
                alert('请选择评价时间！');
                return false;
            }

            if($('.field-commentssearch-content').hasClass("has-error")){
                alert('评论内容不合法！');
                return false;
            }
            var flag = true;
            $.ajax({
                'url':"<?= Yii::$app->urlManager->createUrl(Yii::$app->controller->id . '/prohibited')?>",
                'data':{content:content},
                'type':'post',
                'dataType':'json',
                'async':false,
                'success':function(data){

                    if(data.flag == false){
                        alert(data.msg);
                        flag = false;
                    }

                }
            });
            return flag;
        });



</script>
<?php $this->endBlock(); ?>
